package com.example.demo.controller;

import com.example.demo.dto.LoginDTO;
import com.example.demo.dto.RegisterDTO;
import com.example.demo.dto.ResponseDTO;
import com.example.demo.exception.UserAlreadyExistsException;
import com.example.demo.exception.ValidationException;
import com.example.demo.model.User;
import com.example.demo.service.AuthService;
import com.example.demo.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/auth")
@Validated
@Api(tags = "普通用户接口")
public class AuthController {

    private final AuthService authService;
    private final AuthenticationManager authenticationManager;
    private final JwtTokenUtil jwtTokenProvider;

    public AuthController(
            AuthService authService,
            AuthenticationManager authenticationManager,
            JwtTokenUtil jwtTokenProvider
    ) {
        this.authService = authService;
        this.authenticationManager = authenticationManager;
        this.jwtTokenProvider = jwtTokenProvider;
    }

    /**
     * 用户注册接口
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "注册新用户")
    public ResponseEntity<ResponseDTO<User>> register(
            @Valid @RequestBody RegisterDTO registerDTO
    ) throws ValidationException, UserAlreadyExistsException {
        User registeredUser = authService.registerUser(registerDTO);
        return ResponseEntity.ok(ResponseDTO.success("注册成功", registeredUser));
    }

    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录获取JWT Token")
    public ResponseEntity<ResponseDTO<String>> login(
            @Valid @RequestBody LoginDTO loginDTO  // 使用 LoginDTO
    ) {
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        loginDTO.getUsername(),
                        loginDTO.getPassword()
                )
        );
        SecurityContextHolder.getContext().setAuthentication(authentication);
        String jwtToken = jwtTokenProvider.generateToken((UserDetails) authentication);
        return ResponseEntity.ok(ResponseDTO.success("登录成功", jwtToken));
    }
}